import { _decorator, Component, tween, Vec3} from 'cc';
import { Player } from "./Player";

const { ccclass, property } = _decorator;

@ccclass('PlayerManager') // 用 @ccclass 声明类为 Cocos 组件
export class PlayerManager extends Component {

	static instance: PlayerManager;
    public mySiteIndex = 0;

    onLoad() {
        PlayerManager.instance = this;
        console.log('PlayerManager onLoad!');
    }

    onReset() {
        for(let i = 0; i < 4; i++)
        {
            const player = this.getPlayer(i);
            if(player)
            {
                player.onReset();
            }
        }
    }


    getPlayer(index:number) {
        return this.node.getChildByName("mj"+index)?.getComponent(Player);
    }

    getSelf() {
        return this.getPlayer(this.mySiteIndex);
    }

    setMySite(index:number) {
        this.mySiteIndex = index;
        const player = this.getPlayer(index);
        player?.setMySeat();
    }

    onGameStart() {
        for(let i = 0; i < 4; i++)
        {
            const player = this.getPlayer(i);
            if(player)
            {
                // player.gameStart();
                this.scheduleOnce(()=>{ player.gameStart(); }, 0.1*i);
            }
            // return;
        }
    }

    // onSortCard() {
    //     const self = this.getSelf();
    //     self?.sortStartCards();
    // }

    onGameEnd() {
        for(let i = 0; i < 4; i++)
        {
            const player = this.getPlayer(i);
            player?.getHandCard().showRemainCards();
        }
    }

    // onCombo() {
    //     this.onStartCard();
    //     this.scheduleOnce(this.onSortCard, 1.4);
    //     this.scheduleOnce(this.onShowCard, 3.0);
    // }

    // onDrawCard() {
    //     // const index = Math.floor(Math.random() * 4);
    //     const index = 0;
    //     const self = this.getPlayer(index);
    //     self?.drawCard();
    // }

}